﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace CSRedisCoreDemo
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string redis_conn = @"192.168.0.201:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240";

            // 普通模式
            var csredis = new CSRedis.CSRedisClient(redis_conn);
            // 初始化 RedisHelper
            RedisHelper.Initialization(csredis);

            // 需要切换数据库时，需要这样初始化
            string redie_conn2 = @"192.168.0.201:6379,poolsize=50,ssl=false,writeBuffer=10240";
            var redis = new CSRedis.CSRedisClient[14]; //定义成单例
            for (var a = 0; a < redis.Length; a++)
            {
                redis[a] = new CSRedis.CSRedisClient(redie_conn2 + ",defaultDatabase=" + a);
            }
            redis[0].Ping();

            #region String 键值对
            // 设置值,默认永不过期
            RedisHelper.Set("name", "Harry");
            // 异步操作
            await RedisHelper.SetAsync("name", "Ken").ConfigureAwait(false);
            // 只有key不存在时，才设置
            RedisHelper.SetNx("name", "Ken2");
            RedisHelper.SetNx("name2", "Ken2");
            // 会覆盖同key的值，这里输出是ken
            Console.WriteLine(RedisHelper.Get<String>("name"));
            Console.WriteLine(RedisHelper.Get<String>("name2"));

            // 设置1秒后过期
            RedisHelper.Set("time", DateTime.Now, 1);
            Console.WriteLine(RedisHelper.Get<DateTime>("time"));
            Thread.Sleep(1100);
            Console.WriteLine(RedisHelper.Get<DateTime>("time"));

            // 设置增量
            RedisHelper.Set("count", 1);
            Console.WriteLine(RedisHelper.Get<int>("count"));
            RedisHelper.IncrBy("count");
            Console.WriteLine(RedisHelper.Get<int>("count"));
            #endregion

            #region list 列表(场景：消息队列)
            // 移除列表元素
            RedisHelper.LRem("list", 0, 0);
            // 在列表中添加一个或多个值
            RedisHelper.RPush("list", "第一个元素");
            RedisHelper.RPush("list", "第二个元素");
            RedisHelper.LInsertBefore("list", "第二个元素", "我是新插入的第二个元素！");
            // 获取列表长度
            Console.WriteLine($"list的长度为{ RedisHelper.LLen("list") }");
            // 异步
            Console.WriteLine($"list的长度为{ await RedisHelper.LLenAsync("list").ConfigureAwait(false) }");
            // 获取列表元素
            Console.WriteLine($"list的第1个元素为{ RedisHelper.LIndex("list", 0) }");
            Console.WriteLine($"list的第2个元素为{ RedisHelper.LIndex("list", 1) }");
            // 获取后删掉
            Console.WriteLine($"移出并获取列表的第一个元素:{ RedisHelper.LPop("list") }");
            Console.WriteLine($"移出并获取列表的第一个元素:{ RedisHelper.LPop("list") }");
            Console.WriteLine($"移出并获取列表的第一个元素:{ RedisHelper.LPop("list") }");
            #endregion

            #region Hash 哈希
            // 设置
            RedisHelper.HSet("person", "name", "Ken");
            RedisHelper.HSet("person", "sex", "男");
            RedisHelper.HSet("person", "age", "28");
            // 获取
            Console.WriteLine($"person这个哈希中的age为{ RedisHelper.HGet<int>("person", "age") }");
            var hDic = RedisHelper.HGetAll("person");
            foreach (var item in hDic)
            {
                Console.WriteLine($"key:{ item.Key },value:{ item.Value }");
            }
            #endregion

            #region Set 集合
            RedisHelper.SAdd("students", "harry", "ken");
            RedisHelper.SAdd("students", "kyo");
            RedisHelper.SAdd("students", "lily");
            Console.WriteLine($"students这个集合的大小为{ RedisHelper.SCard("students") }");

            RedisHelper.SAdd("teachers", "harry", "ken", "lion");
            Console.WriteLine($"teachers这个集合的大小为{ RedisHelper.SCard("teachers") }");

            // 差集
            var SDiffArray = RedisHelper.SDiff("students", "teachers");
            foreach (var item in SDiffArray)
            {
                Console.WriteLine($"students和teachers的差集：{ item }");
            }

            // 交集
            var SInterArray = RedisHelper.SInter("students", "teachers");
            foreach (var item in SInterArray)
            {
                Console.WriteLine($"students和teachers的交集：{ item }");
            }

            // 并集
            var SUnionArray = RedisHelper.SUnion("students", "teachers");
            foreach (var item in SUnionArray)
            {
                Console.WriteLine($"students和teachers的并集：{ item }");
            }

            #endregion

            #region 订阅与发布
            // 订阅
            var chan1 = RedisHelper.Subscribe(("chan1", msg => Console.WriteLine(msg.Body)));
            var chan2 = RedisHelper.Subscribe(("chan2", msg => Console.WriteLine(msg.Body)));
            var chan3 = RedisHelper.Subscribe(("chan3", msg => Console.WriteLine(msg.Body)));
            // 取消订阅
            chan3.Unsubscribe();
            //发布
            RedisHelper.Publish("chan1", "123");
            RedisHelper.Publish("chan2", "456");
            RedisHelper.Publish("chan3", "789");
            #endregion

            #region 缓存壳
            // key-value
            var t1 = RedisHelper.CacheShell("test1", 10, () => { return "1"; });
            Console.WriteLine(RedisHelper.Get("test1"));
            // set
            var t2 = RedisHelper.CacheShell("test2", "1", 10, () => { return "test2"; });
            var hDic2 = RedisHelper.HGetAll("test2");
            foreach (var item in hDic2)
            {
                Console.WriteLine($"key:{ item.Key },value:{ item.Value }");
            }
            #endregion

            #region 管道,打包多条命令一起执行，从而提高性能
            var pipe1 = RedisHelper.StartPipe();
            pipe1.Set("name", "kyo");
            pipe1.HSet("person", "name", "Ken");
            pipe1.EndPipe();

            Console.WriteLine(RedisHelper.Get<String>("name"));
            Console.WriteLine($"person这个哈希中的name为{ RedisHelper.HGet<string>("person", "name") }");
            #endregion


            Console.ReadKey();
        }
    }
}
